package com.xk._03真题骗._01数组_排序;

import java.util.Arrays;

/*
 * @description: https://leetcode.cn/problems/merge-sorted-array/
 * @author: xu
 * @date: 2022/10/27 5:33
 */
public class _88合并两个有序数组 {
    public static void main(String[] args) {
        int[] nums1 = new int[]{1,2,3,0,0,0};
        int[] nums2 = new int[]{2,5,6};
        System.out.println(Arrays.toString(nums1));
        new _88合并两个有序数组().merge(nums1, 3, nums2, 3);
        System.out.println(Arrays.toString(nums1));
    }

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int i1 = m - 1;
        int i2 = n - 1;
        int cur = nums1.length - 1;
        while (i2 >= 0) {
            if (i1 >= 0 && nums1[i1] > nums2[i2]){
                nums1[cur--] = nums1[i1--];
            } else {
                // i1 < 0 || nums1[i1] <= nums2[i2]
                nums1[cur--] = nums2[i2--];
            }
        }
    }
}
